package clue.players;

import java.util.Scanner;

import org.javatuples.Triplet;

import clue.cards.Card;
import clue.engine.GameState;
import clue.enums.Room;
import clue.enums.Suspect;
import clue.enums.Weapon;

public class HumanPlayer extends Player {

	private Scanner scanner;
	public HumanPlayer() {
		scanner = new Scanner(System.in);
	}
	
	@Override
	public void gameStarted(GameState gs) {
		//System.out.println("=====================================");
		System.out.println("Game Started.");
		//System.out.println("You are player " + gs.getOwnID(this));
		System.out.println("The order of players is: ");
		for (Integer id : gs.playerIDs) {
			System.out.print(id + " ");
		}
		System.out.println("\nYour cards are:");
		for (Card c : gs.getOwnHand(this)) {
			System.out.println(c);
		}
	}

	@Override
	public Room move(GameState gs) {
		//System.out.println("=====================================");
		//System.out.println("You are player " + gs.getOwnID(this));
		System.out.println("Which room to move to?");
		System.out.print("Rooms: ");
		for (Room r : Room.values()) {
			System.out.print(r + " ");
		}
		System.out.println();
		Room room = null;
		do {
			room = Room.valueOf(scanner.nextLine().trim().toUpperCase());
		} while (room == null);
		return room;
	}

	@Override
	public Triplet<Room, Suspect, Weapon> suggest(GameState gs) {
		//System.out.println("=====================================");
		//System.out.println("You are player " + gs.getOwnID(this));
		System.out.println("Make a suggestion:");
		System.out.print("Rooms: ");
		for (Room r : Room.values()) {
			System.out.print(r + " ");
		}
		System.out.println();
		System.out.print("Suspects: ");
		for (Suspect s : Suspect.values()) {
			System.out.print(s + " ");
		}
		System.out.println();
		System.out.print("Weapons: ");
		for (Weapon w : Weapon.values()) {
			System.out.print(w + " ");
		}
		System.out.println();
		Room room = null;
		Suspect suspect = null;
		Weapon weapon = null;
		do {
			String line = scanner.nextLine().trim();
			String[] rws = line.split("\\s+");
			room = Room.valueOf(rws[0].toUpperCase());
			suspect = Suspect.valueOf(rws[1].toUpperCase());
			weapon = Weapon.valueOf(rws[2].toUpperCase());
		} while (room == null || suspect == null || weapon == null);
		return new Triplet<Room, Suspect, Weapon>(room, suspect, weapon);
	}

	@Override
	public Card refute(GameState gs, Integer suggestingPlayerID,
			Triplet<Room, Suspect, Weapon> suggestion) {
		//System.out.println("=====================================");
		//System.out.println("You are player " + gs.getOwnID(this));
		System.out.println("The suggestion is: " + suggestion);
		System.out.print("Your cards are: ");
		for (Card c : gs.getOwnHand(this)) {
			System.out.print(c + " ");
		}
		System.out.println("\nRefute:");
		int index = -1;
		index = Integer.parseInt(scanner.nextLine().trim());
		if (index < 0)
			return null;
		return (Card)gs.getOwnHand(this).toArray()[index];
	}

	@Override
	public void informMove(GameState gs, Integer movingPlayerID, Room room) {
		//System.out.println("=====================================");
		//System.out.println("You are player " + gs.getOwnID(this));
		System.out.println("Player " + movingPlayerID + " moved into room " + room);
	}

	@Override
	public void informSuggestion(GameState gs, Integer suggestingPlayerID,
			Triplet<Room, Suspect, Weapon> suggestion) {
		//System.out.println("=====================================");
		//System.out.println("You are player " + gs.getOwnID(this));
		System.out.println("Player " + suggestingPlayerID + " suggested " + suggestion);
	}

	@Override
	public void informFailureToRefute(GameState gs, Integer failedPlayerID,
			Triplet<Room, Suspect, Weapon> suggestion) {
		//System.out.println("=====================================");
		//System.out.println("You are player " + gs.getOwnID(this));
		System.out.println("Player " + failedPlayerID + " failed to refute " + suggestion);
	}

	@Override
	public void informRefutation(GameState gs, Integer refutingPlayerID,
			Card refutingCard) {
		//System.out.println("=====================================");
		//System.out.println("You are player " + gs.getOwnID(this));
		System.out.println("Player " + refutingPlayerID + " refuted with " + refutingCard);
	}

	@Override
	public void informRefutationHidden(GameState gs, Integer refutingPlayerID,
			Integer suggestingPlayerID,
			Triplet<Room, Suspect, Weapon> suggestion) {
		//System.out.println("=====================================");
		//System.out.println("You are player " + gs.getOwnID(this));
		System.out.println("Player " + refutingPlayerID + " refuted player " + suggestingPlayerID + ".");
	}

	@Override
	public void gameEnded(GameState gs, Integer winnerID,
			Triplet<Room, Suspect, Weapon> winningAccusation) {
		//System.out.println("=====================================");
		//System.out.println("You are player " + gs.getOwnID(this));
		if (winningAccusation == null) {
			System.out.println("Game ended. Player " + winnerID + " wins by default.");
		} else {
			System.out.println("Game ended. Player " + winnerID + " wins with accusation " + winningAccusation);
		}
	}

	@Override
	public void informFailedAccusation(GameState gs, Integer failedPlayerID,
			Triplet<Room, Suspect, Weapon> failedAccusation) {
		//System.out.println("=====================================");
		//System.out.println("You are player " + gs.getOwnID(this));
		System.out.println("Player " + failedPlayerID + " failed accusation " + failedAccusation);
	}

	@Override
	public Triplet<Room, Suspect, Weapon> accuse(GameState gs) {
		//System.out.println("=====================================");
		//System.out.println("You are player " + gs.getOwnID(this));
		System.out.println("Accusation?");
		String response = scanner.nextLine().trim();
		if ("no".equals(response.toLowerCase()) || "n".equals(response.toLowerCase())) {
			return null;
		} else if ("yes".equals(response.toLowerCase()) || "y".equals(response.toLowerCase())) {
			Room room = null;
			Suspect suspect = null;
			Weapon weapon = null;
			do {
				String line = scanner.nextLine();
				String[] rws = line.split("\\s+");
				room = Room.valueOf(rws[0].toUpperCase());
				suspect = Suspect.valueOf(rws[1].toUpperCase());
				weapon = Weapon.valueOf(rws[2].toUpperCase());
			} while (room == null || suspect == null || weapon == null);
			return new Triplet<Room, Suspect, Weapon>(room, suspect, weapon);
		}
		return null;
	}

}
